%% sphygmaCorDataProcessing
%
% This function will take in the SphygmaCor files and export a fitted curve for input into an fft
% Outputs of this script are time vector, pressure vector in mmHg, age,
% heartrate, sampleing frequecy, and period of signal
%
%
%% Syntax
% [TimeOriginal, TimeFinal, Pressure Values, Signal Period] = sphygmaCorDataProcessing(PressureDataMatrix, SamplingFrequency, HeartRate)
%
% [t_o,t_f, ydata, Tp] = sphygmaCorDataProcessing(data, samplingfreq, heartrate)
%
%
%% Description
% This function will take in the SphygmaCor pressure data and export a fitted curve for input into the fft
% Inputs are a vector of pressure data , sampleing frequency of
% the signal and heart rate of the patient.
% Outputs of this script are time vector, pressure vector in mmHg, and
% signal period
%
%% Arguments
%* data                          - Double: A nx1 double of non-invasive
%                                        central pressure values
%
%* samplingfreq                  - Double: A double precision number of
%                                        sampling frequency of the signal
%
%* heartrate                     - Double: A double precision number of
%                                        patient heartrate 
%
%% Returns
%* TimeOriginal                   - Double: A nx1 double precision vector of
%                                        the original time values
%
%* TimeFinal                      - Double: A nx1 double precision vector of
%                                        final time values 
%
%* FinalPressureValues           - Double: A nx128 double precision vector
%                                        of pressure data values
%                                        
%* Tp                            - Double: A single value of the total
%                                       period of the signal
%
%% Function Side Effects
%* N/A
%
%% Exceptions
%* An error is thrown if data type of input varaibles is not compatible
% with what the function is expecting
%
%% See Also
% N/A


function [TimeOrignal, TimeFinal, FinalPressureValues, Tp] = sphygmaCorDataProcessing(data, samplingfreq, heartrate)




%% Step 0 Parse Data and Import Needed Data






%% Logic Check on correct input data
   
    if isa(data,'double') ~= 1
          error('The pressure measurement should only be a vector of pressure values of type double' ) ;  
    end
    
   if isa(samplingfreq,'double') ~= 1
          error('The sampling frequency should be a single value type double' )     
   end
    
    if isa(heartrate,'double') ~= 1
          error('The heart rate should be a single value type double' ) ;
    end
     	
%% Inputs

sampling_time = SamplingFreqHzToSec(samplingfreq); 

TimeOrignal = generateSignalTimeVector(sampling_time, data); % Add a time column to the pressure data file

ydata0 = convertmmHgToDynescm2(data); % Convert from mmHg to dynes/cm2 
    
Tp = calcSignalPeriod(heartrate); % Calculate Period of Signal (1/(HR/60))

npts = 128; % Number of points used for resampling



%% STEP 1 change signal so that it is equisampled and smooth from one cycle to the next


[T_Extended, P_Exteneded] = generatePeriodicExtension(ydata0, TimeOrignal, Tp);
  
%--- fit a curve to it and then resample ---%

[TimeFinal, FinalPressureValues] = curveFitandResample(TimeOrignal, T_Extended, P_Exteneded, npts, Tp);


end